//
//  GKZYMyInfoCell.m
//  xxptmobile
//
//  Created by KwokChun Cheung on 15/11/3.
//  Copyright © 2015年 gzedu. All rights reserved.
//

#import "GKZYMyInfoCell.h"
#import "GKZYMyInfoModel.h"
#import "xxptmobile.h"

@interface GKZYMyInfoCell()
@property (nonatomic,strong) UILabel *topLine;
@property (nonatomic,strong) UILabel *bottomLine;
@property (nonatomic,strong) UISegmentedControl *segment;
@property (nonatomic,strong) UILabel *check;
@end

@implementation GKZYMyInfoCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    return self;
}



-(void)layoutSubviews{
    
    [super layoutSubviews];
    
    if (!self.hiddenTopLine) {
        [self addSubview:self.topLine];
        [self.topLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self);
            make.height.equalTo(@(0.5));
        }];
    }
    
    if (!self.hiddenBottomLine) {
        [self addSubview:self.bottomLine];
        [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.equalTo(self);
            make.height.equalTo(@(0.5));
        }];
    }
    
    if (self.data.showArrows) {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else{
        self.accessoryType = UITableViewCellAccessoryNone;
    }
    
    if (self.data.segArray && self.data.segArray.count>0) {
        [self addSubview:self.segment];
        [self.segment mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.right.equalTo(self).offset(-15);
            make.height.equalTo(@(32));
            make.width.equalTo(@(90));
        }];
    }
    
    if (self.data.isCheck) {
        UITapGestureRecognizer *reg = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(checkToggleForReg:)];
        [self addGestureRecognizer:reg];
        [self addSubview:self.check];
        [self.check mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.right.equalTo(self).offset(-15);
            make.width.height.equalTo(@(15));
        }];
    }
    
    if (self.data.isImg) {
        CGFloat imgUnit = 48;
        self.imageView.layer.cornerRadius = imgUnit/2;
        self.imageView.layer.masksToBounds = YES;
        self.imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.imageView.layer.borderWidth = 1;
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self.imageView setFrame:CGRectMake(20, (self.frame.size.height/2)-(imgUnit/2) ,imgUnit, imgUnit)];
    }
}

-(void)checkToggleForReg:(UIGestureRecognizer *)reg{
    [self checkToggle:self.check];
}

-(void)checkToggle:(UILabel *)check{
    if (!check) { return; }
    if (!check.tag && check.tag>=0) {
        check.tag = 0;
    }
    if (check.tag == 1) {
        check.layer.borderWidth = 0;
        check.backgroundColor = [UIColor colorWithRed:49/255.f green:146/255.f blue:254/255.f alpha:1];
        check.font = [UIFont fontWithName:FONT_FONTAWESOME size:12];
        check.text = @"\U0000F00C";
        check.textAlignment = NSTextAlignmentCenter;
        check.textColor = [UIColor whiteColor];
        check.tag = 0;
    }else if (check.tag == 0){
        check.layer.borderWidth = 1;
        check.layer.borderColor = [UIColor lightGrayColor].CGColor;
        check.backgroundColor = [UIColor whiteColor];
        check.tag = 1;
    }
}

#pragma mark - Setter For Property

-(UILabel *)topLine{
    if (!_topLine) {
        _topLine = ({
            UILabel *topLine = [UILabel new];
            topLine.backgroundColor = [UIColor lightGrayColor];
            topLine;
        });
    }
    return _topLine;
}

-(UILabel *)bottomLine{
    if (!_bottomLine) {
        _bottomLine = ({
            UILabel *bottomLine = [UILabel new];
            bottomLine.backgroundColor = [UIColor lightGrayColor];
            bottomLine;
        });
    }
    return _bottomLine;
}

-(void)setData:(GKZYMyInfoModel *)data{
    UIFont *wordFont = [UIFont systemFontOfSize:16];
    
    self.textLabel.text = data.title;
    self.textLabel.font = wordFont;
    self.detailTextLabel.text = data.subTitle;
    self.detailTextLabel.font = wordFont;
    
    if(data.img){
        self.imageView.image = data.img;
    }else if (data.imgName) {
        self.imageView.image = [ResourceHelper archiveImage:data.imgName withType:data.imgSuffix];
    }
    
    _data = data;
}

#pragma mark - Getter For Views

-(UISegmentedControl *)segment{
    if (!_segment) {
        _segment = ({
            UISegmentedControl *seg = [UISegmentedControl new];
            [seg setTintColor:[UIColor colorWithRed:49/255.f green:146/255.f blue:254/255.f alpha:1]];
            if (self.data.segArray && self.data.segArray.count>0) {
                [self.data.segArray enumerateObjectsUsingBlock:^(NSString *  _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
                   [seg insertSegmentWithTitle:title atIndex:idx animated:YES];
                }];
            }
            seg.selectedSegmentIndex = 0;
            seg.selected = YES;
            seg;
        });
    }
    return _segment;
}

-(UILabel *)check{
    if (!_check) {
        _check = ({
            UILabel *check = [UILabel new];
            check.tag = self.data.isChecked ? 1 : 0;
            [self checkToggle:check];
            check;
        });
    }
    return _check;
}

@end

